package cn.itsource.zcn.quartz.service;

import cn.itsource.zcn.order.domain.OrderAdopt;
import cn.itsource.zcn.order.mapper.OrderAdoptMapper;
import cn.itsource.zcn.pay.domain.PayBill;
import cn.itsource.zcn.pay.mapper.PayBillMapper;
import cn.itsource.zcn.pet.domain.Pet;
import cn.itsource.zcn.pet.mapper.PetMapper;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class MyJob implements Job {
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private OrderAdoptMapper orderAdoptMapper;

    @Autowired
    private PetMapper petMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 拿出订单号
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        Map<String,Object> map = (Map<String, Object>) jobDataMap.get("params");
        String orderSn = (String) map.get("orderSn");

        //根据订单号查询
        PayBill payBill = payBillMapper.queryByOrderSn(orderSn);
        payBill.setState(-1);
        payBill.setUpdateTime(new Date());
        payBillMapper.update(payBill);

        // 领养订单
        OrderAdopt orderAdopt = orderAdoptMapper.queryByOrderSn(orderSn);
        orderAdopt.setState(-1);
        orderAdoptMapper.update(orderAdopt);
        //宠物上架
        Pet pet = petMapper.queryById(orderAdopt.getPet_id());
        pet.setState(1L);
        pet.setOnsaletime(new Date());
        petMapper.update(pet);

    }
}
